package com.driden.way;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AddrListActivity extends Activity{

	ArrayList<Address> addrs = new ArrayList<Address>();
			
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address_list);
        
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        addrs = bundle.getParcelableArrayList("addrs");
        
        ListView lv = (ListView) findViewById(R.id.listView1);
        
        AddressAdapter adapter = new AddressAdapter();
        lv.setAdapter(adapter);
        
        lv.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
			}
        	
        });
    }
    
    public class AddressAdapter extends BaseAdapter{
    	public int getCount() {
    		return addrs.size();
    	}

    	public Object getItem(int position) {
    		Address y = addrs.get(position);
    		return y;
    	}

    	public long getItemId(int position) {
    		
    		return 0;
    	}

    	public View getView(int position, View convertView, ViewGroup parent) {
    		LayoutInflater inflater = getLayoutInflater();
    		View view = inflater.inflate(R.layout.addrs_item, null);
    		Address address = (Address) getItem(position);
    		TextView tv1 = (TextView) view.findViewById(R.id.textView1);
    		TextView tv2 = (TextView) view.findViewById(R.id.textView2);
    		
    		String addr1 = "";
    		String addr2 = "";
    		String addr3 = "";
    		String addr4 = "";
    		String addr5 = "";
    		
    		if (address.getAdminArea()==null){
    			addr1 = "";
    		}else{
    			addr1 = address.getAdminArea()+" ";
    		}
    		if (address.getLocality()==null){
    			addr2 = "";
    		}else{
    			addr2 = address.getLocality()+ " ";
    		}
    		if (address.getSubLocality()==null){
    			addr3 = "";
    		}else{
    			if (address.getSubLocality().equals(address.getLocality())){
    				addr3 = "";
    			}
    			addr3 = address.getSubLocality()+ " ";
    		}
    		if (address.getPostalCode()==null){
    			addr4 = "";
    		}else{
    			addr4 = address.getPostalCode()+ " ";
    		}
    		if (address.getFeatureName()==null){
    			addr5 = "";
    		}else{
    			addr5 = address.getFeatureName();
    		}
    		tv1.setText(addr1 + addr2 + addr3 + addr4 + addr5);
    		tv2.setText(address.getLatitude() + " / " + address.getLongitude());
    		return view;
    	}
    	
    }
}
